package com.hr.humanresource.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hr.LandR.domain.LeadershipRelationship;
import com.hr.LandR.mapper.LeadershipRelationshipMapper;
import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.mapper.BorrowingMapper;
import com.hr.common.utils.DateUtils;
import com.hr.humanresource.domain.SysUserVO;
import com.hr.jobTimeRecord.domain.JobTimeRecord;
import com.hr.jobTimeRecord.mapper.JobTimeRecordMapper;
import com.hr.system.domain.SysUserPost;
import com.hr.system.domain.SysUserRole;
import com.hr.system.mapper.SysUserMapper;
import com.hr.system.mapper.SysUserPostMapper;
import com.hr.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hr.humanresource.mapper.humanresourceMapper;
import com.hr.common.core.domain.entity.SysUser;
import com.hr.humanresource.service.humanresourceService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 员工管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-14
 */
@Service
public class humanresourceServiceImpl implements humanresourceService
{
    @Resource
    private humanresourceMapper humanresourceMapper;

    @Resource
    private BorrowingMapper borrowingMapper;

    @Resource
    private JobTimeRecordMapper jobTimeRecordMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Resource
    private LeadershipRelationshipMapper LRMapper;

    /**
     * 查询员工信息
     *
     * @param userId 员工管理主键
     * @return 员工管理
     */
    @Override
    public SysUserVO selectSysUserByUserId(Long userId)
    {
        return humanresourceMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询员工管理列表
     *
     * @param sysUser 员工管理
     * @return 员工管理
     */
    @Override
    public List<SysUserVO> selectSysUserList(SysUser sysUser)
    {
        return humanresourceMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增员工管理
     * 
     * @param sysUser 员工管理
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser)
    {
        sysUser.setCreateTime(DateUtils.getNowDate());
        return humanresourceMapper.insertSysUser(sysUser);
    }

    /**
     * 修改员工管理
     * 
     * @param sysUserVO 员工管理
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUserVO sysUserVO)
    {
//        1.修改员工工种
        Long roleId = sysUserVO.getRoleId();
        Long userId = sysUserVO.getUserId();
        if (roleId != null) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleMapper.updateUserRoleById(userRole);
        }
//        2.修改员工岗位
        userPostMapper.deleteUserPostByUserId(userId);
        List<Long> postIds = sysUserVO.getPostIds();
        List<SysUserPost> list = new ArrayList<>(postIds.size());
        for(Long id : postIds){
            SysUserPost userPost = new SysUserPost();
            userPost.setUserId(userId);
            userPost.setPostId(id);
            list.add(userPost);
        }
        userPostMapper.batchUserPost(list);
//        3.修改员工借支信息
        Integer flag = sysUserVO.getFlag();
        Borrowing borrowing = new Borrowing();
        borrowing.setUserId(userId);
        borrowing.setAmount(sysUserVO.getAmount());
        borrowing.setNumber(sysUserVO.getNumber());
//            判断借支权限flag，若为“1”则往借支表插入数据
        if (flag.equals(1) && borrowingMapper.selectBorrowingByUserId(userId) != null){
            borrowingMapper.updateBorrowing(borrowing);
        }else if (flag.equals(2) && borrowingMapper.selectBorrowingByUserId(userId) != null){
//                若为“2”且有借支权限，则取消该用户的借支权限
            borrowingMapper.deleteBorrowingByUserId(userId);
        }else {
            borrowingMapper.insertBorrowing(borrowing);
        }
//        4.修改员工其他信息
        sysUserVO.setUpdateTime(DateUtils.getNowDate());
//        5.修改员工上级
        Long leaderId = sysUserMapper.selectUserIdByNickName(sysUserVO.getLeaderNickName());
        LeadershipRelationship leadershipRelationship = new LeadershipRelationship(userId,leaderId);
        LRMapper.updateLeadershipRelationship(leadershipRelationship);
        return humanresourceMapper.updateSysUser(sysUserVO);
    }

    /**
     * 批量删除员工管理
     * 
     * @param userIds 需要删除的员工管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUserByUserIds(Long[] userIds)
    {
        try {
            for (Long userId : userIds){
    //            将用户身份改为求职者
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(2L);
                userRoleMapper.updateUserRoleById(userRole);
    //            取消用户借支权限和归属部门
                SysUser sysUser = new SysUser();
                sysUser.setUserId(userId);
                sysUser.setFlag(2);
                sysUser.setDeptId(0L);
                sysUser.setIdleState(1);
                sysUserMapper.updateSysUser(sysUser);
    //            设置员工离职时间
                JobTimeRecord JobTimeRecord = new JobTimeRecord();
                JobTimeRecord.setUserId(userId);
                JobTimeRecord.setResignationTime(DateUtils.getNowDate());
                JobTimeRecord.setFlag(2);
                jobTimeRecordMapper.updateJobTimeRecord(JobTimeRecord);
    //            删除员工归属岗位
                return userPostMapper.deleteUserPostByUserId(userId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

}
